package com.brotherly.mpgp;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.brotherly.framework.BaseActivity;
import com.brotherly.framework.service.ServiceManager;
import com.brotherly.mpgp.remoteinterface.RemoteServerService;

public class SuggestionActiviy extends BaseActivity
{
    private View mSubmit = null;
    private EditText mContent = null;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_suggestion);

        mSubmit = findViewById(R.id.activity_suggestion_submit);
        mSubmit.setOnClickListener(mSubmitListener);

        mContent = (EditText) findViewById(R.id.activity_suggestion_sug);
    }

    private OnClickListener mSubmitListener = new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            String content = mContent.getText().toString().trim();
            if (content == null || content.length() <= 0)
            {
                showDialog("信息", "请输入信息后提交！", null);
                return;
            }
            showProgressDialog("正在提交数据，请稍候！");
            submitContent(content);
        }
    };

    private void submitContent(final String content)
    {
        RemoteServerService serverService = (RemoteServerService) ServiceManager.getInstance().getService(
                ServiceManager.REMOTESEVER_SERVICE);
        serverService.publishSuggestion(content);
    }

    private AlertDialog showDialog(String title, String content, android.content.DialogInterface.OnClickListener listener)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
        builder.setMessage(content);
        builder.setPositiveButton("确定", listener);
        AlertDialog dialog = builder.create();
        dialog.show();
        return dialog;
    }

    private void onSendSuggestionFinish(int stateCode)
    {
        if (stateCode == 200)
        {
            android.content.DialogInterface.OnClickListener listener = new android.content.DialogInterface.OnClickListener()
            {

                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    finish();
                }
            };
            showDialog("信息", "感谢您的建议，我们会努力做得更好！", listener);
        } else
        {
            showDialog("信息", "发送失败！", null);
        }
    }

    @Override
    protected void onServiceEvent(String id, Object param1, Object param2)
    {
        hideProgressDialog();
        if (RemoteServerService.RemoteServer_Event_RemoteServer_Success.equals(id)
                && RemoteServerService.ACTION_SUGGESTION_COMMIT.equals(param1))
        {
            runOnUiThread(new Runnable()
            {

                @Override
                public void run()
                {
                    onSendSuggestionFinish(200);
                }
            });
        }
        else if (RemoteServerService.RemoteServer_Event_RemoteServer_Error.equals(id)
                && RemoteServerService.ACTION_SUGGESTION_COMMIT.equals(param1))
        {
            runOnUiThread(new Runnable()
            {

                @Override
                public void run()
                {
                    onSendSuggestionFinish(-1);
                }
            });
        }
    }

    @Override
    public void onAllReady()
    {

    }
}
